package com.jz.community.modulemine.money.model;

import android.content.Context;

import com.jz.community.basecomm.bean.BaseResponseInfo;
import com.jz.community.basecomm.net.retrofit.ApiException;
import com.jz.community.basecomm.net.retrofit.ApiUtils;
import com.jz.community.basecomm.net.retrofit.HttpRxObservable;
import com.jz.community.basecomm.net.retrofit.JsonRequestBodyConverter;
import com.jz.community.basecomm.net.retrofit.RxLoadingWrapper;
import com.jz.community.basecomm.net.rxandroid.OnLoadListener;
import com.jz.community.basecomm.utils.LoggerUtils;
import com.jz.community.modulemine.money.bean.MoneyActiveBean;
import com.jz.community.modulemine.money.bean.MoneyDetailBean;
import com.jz.community.modulemine.money.bean.MoneyExchangeCodeBean;
import com.jz.community.modulemine.money.bean.MoneyInfoBean;
import com.jz.community.modulemine.money.bean.MoneyRechargeBean;
import com.jz.community.modulemine.money.bean.PurchaseInfoBean;
import com.jz.community.modulemine.money.net.MoneyNetApi;
import com.trello.rxlifecycle2.components.support.RxFragmentActivity;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.functions.Consumer;

public class MoneyModelImp implements MoneyModel {

    private Context mContext;

    public MoneyModelImp(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void initMoneyData(OnLoadListener<MoneyInfoBean> onLoadListener) {
        Observable<MoneyInfoBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MoneyNetApi.class)
                .initMoneyData());
        new RxLoadingWrapper<MoneyInfoBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void initMoneyDetailData(int page, int size, OnLoadListener<MoneyDetailBean> onLoadListener) {
        Observable<MoneyDetailBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MoneyNetApi.class)
                .initMoneyDetailData(page, size));
        new RxLoadingWrapper<MoneyDetailBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void initMoneyActive(OnLoadListener<MoneyActiveBean> onLoadListener) {
        Observable<MoneyActiveBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MoneyNetApi.class)
                .initMoneyActive());
        new RxLoadingWrapper<MoneyActiveBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));
    }

    @Override
    public void reChargeMoney(String type, double money, OnLoadListener<MoneyRechargeBean> onLoadListener) {
        Observable<MoneyRechargeBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MoneyNetApi.class)
                .rechargeMoney(type, money));
        new RxLoadingWrapper<MoneyRechargeBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void initCodeInfo(String code, OnLoadListener<MoneyExchangeCodeBean> onLoadListener) {
        Observable<MoneyExchangeCodeBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MoneyNetApi.class)
                .initCodeInfo(code));
        new RxLoadingWrapper<MoneyExchangeCodeBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void exchangeMoney(String code, OnLoadListener<BaseResponseInfo> onLoadListener) {
        Observable<BaseResponseInfo> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MoneyNetApi.class)
                .exchangeMoney(code));
        new RxLoadingWrapper<BaseResponseInfo>(mContext,false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void initPurchaseInfo(OnLoadListener<PurchaseInfoBean> onLoadListener) {
        Observable<PurchaseInfoBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MoneyNetApi.class)
                .initPurchaseInfo());
        new RxLoadingWrapper<PurchaseInfoBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }

    @Override
    public void purchaseMoney(String company, String count, String name, String phone, String address, String mail, OnLoadListener<PurchaseInfoBean> onLoadListener) {
        Map<String, String> params = new HashMap<>();
        params.put("company", company);
        params.put("count", count);
        params.put("name", name);
        params.put("phone", phone);
        params.put("address", address);
        params.put("email", mail);


        Observable<PurchaseInfoBean> observer = HttpRxObservable.getObservable((RxFragmentActivity) mContext, ApiUtils.getApi(mContext, MoneyNetApi.class)
                .purchaseMoney(new JsonRequestBodyConverter<Map<String, String>>().convert(params)));
        new RxLoadingWrapper<PurchaseInfoBean>(mContext, false)
                .execute(observer)
                .subscribe(resultBean -> {
                    if (resultBean == null) {
                        return;
                    }
                    onLoadListener.onSuccess(resultBean);
                    LoggerUtils.fLog().i("ceshi " + resultBean.toString());
                }, throwable -> onLoadListener.onFail(throwable.toString(), ApiException.handleException(throwable).code));

    }


}
